﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibData.Repository;
using StudyEnglish.Model;

namespace StudyEnglish.Service.Implementation
{
    class FileService : IFileService
    {
        protected IRepository<File> fileRepo = RepositoryFactory.Create<File>();

        public void SaveFile(File file)
        {
            file.UploadDate = DateTime.Now;
            fileRepo.SaveNew(file);
        }

        
        public IList<File> FindAllFiles()
        {
            return fileRepo.FindAll();
        }

        public IQueryable<File> FilesLinq
        {
            get { return fileRepo.Linq; }
        }

        public File GetFileById(int value)
        {
            return fileRepo.FindById(value);
        }

        public void UpdateFile(File file)
        {
            fileRepo.SaveOrUpdate(file);
        }

        public void DeleteFile(File file)
        {
            fileRepo.Delete(file);
        }

        public File FindByName(string filename)
        {
            return (from f in fileRepo.Linq where f.InitialFileName == filename select f).FirstOrDefault();
        }

        public IList<File> FindAllImportedFiles()
        {
            return (from f in fileRepo.Linq where f.InitialFilePath != null select f).ToList();
        }
    }
}
